package top.drinyeit.nongmao.presenter;

import android.support.v7.widget.LinearLayoutManager;

import java.util.List;
import java.util.Map;

import top.drinyeit.nongmao.activity.CategoryActivity;
import top.drinyeit.nongmao.adapter.CategoryAdapter;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/9.
 */

public class CategoryPresenter {
    int p,s;
    private CategoryActivity activity;
    public CategoryPresenter(CategoryActivity activity){
        this.activity = activity;
    }

    private CategoryAdapter categoryAdapter;

    //获取分类后详情
    public void categorySearch(Map<String,Object> map) {
        final int index = (int) map.get("page_index");
        final int priceType = map.get("price_type") == null ? 0 : (int) map.get("price_type");
        final int selxlType = map.get("sell_type") == null ? 0 : (int) map.get("sell_type");
        p = priceType;
        s = selxlType;
        CommonSubscriber<HttpResponse<List<CategoryGoodsDetailsBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<CategoryGoodsDetailsBean>>>() {
            @Override
            public void onError(String e, int code) {
                activity.shSwipeRefreshLayout.finishLoadmore();
                activity.shSwipeRefreshLayout.finishRefresh();
                activity.showToastMsg("错误，请重新操作!");
            }

            @Override
            public void onNext(final HttpResponse<List<CategoryGoodsDetailsBean>> o) {
                activity.shSwipeRefreshLayout.finishLoadmore();
                activity.shSwipeRefreshLayout.finishRefresh();
                if (o.getCode() == COMMON_SUCCESS) {
                    if (p != priceType || s != selxlType) {
                        categoryAdapter = new CategoryAdapter(o.getData(),null);
                    }
                    activity.pageIndex = index;
                    if (index == 1) {
                        if (categoryAdapter == null) {
                            if (o.getData() != null && o.getData().size() > 0) {
                                categoryAdapter = new CategoryAdapter(o.getData(),null);
                                activity.mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
                                activity.mRecyclerView.setAdapter(categoryAdapter);
                            }
                        } else {
                            if (o.getData() == null || o.getData().size() == 0) {
                                categoryAdapter.clear();
                            } else {
                                categoryAdapter.setData(o.getData());
                            }
                            categoryAdapter.notifyDataSetChanged();
                        }
                    } else if (index > 1) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            categoryAdapter.addData(o.getData());
                            categoryAdapter.notifyItemRangeInserted(categoryAdapter.getData().size(), o.getData().size());
                        } else {
                            activity.showToastMsg("没有更多啦");
                        }
                    }
                }else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().categorySearch(subscriber, map);
    }
}
